<template>
	<view>
		<zmm-watermark :watermark="userStore.watermark" v-if="userStore.watermark"></zmm-watermark>
		<!--导航栏-->
		<pageLayout
			:barTitle="title"
			:isSearchNull="isSearchNull"
			:isOpen="isOpen"
			@rightClick="clickSearch">
		</pageLayout>
		<!--查询弹窗-->
		<up-popup :z-index="90" :show="isOpen" @close="closeSearch" mode="top">
			<scroll-view class="search" scroll-y="true" @touchmove.stop>
				<view class="search-form">
					<uni-forms ref="searchRef" :modelValue="searchModel" label-width="160rpx">
						<uni-forms-item label="区域">
							<uni-data-select
								v-model="searchModel.regionCode"
								placeholder="请选择区域"
								:localdata="setStore.allProvinceList"></uni-data-select>
						</uni-forms-item>
						<uni-forms-item label="状态">
							<uni-data-select
								v-model="searchModel.status"
								placeholder="请选择状态"
								:localdata="orderRefundStatusList"></uni-data-select>
						</uni-forms-item>
						<uni-forms-item label="退款单号">
							<uni-easyinput v-model="searchModel.number" placeholder="请输入退款单号" />
						</uni-forms-item>
						<uni-forms-item label="仓库名称">
							<uni-easyinput v-model="searchModel.warehouseName" placeholder="请输入仓库名称" />
						</uni-forms-item>
						<uni-forms-item label="订单编号">
							<uni-easyinput v-model="searchModel.orderNumber" placeholder="请输入订单编号" />
						</uni-forms-item>
						<uni-forms-item label="销售人员">
							<uni-easyinput v-model="searchModel.salesmanName" placeholder="请输入销售人员" />
						</uni-forms-item>
					</uni-forms>
					<view class="search-footer main-flex">
						<up-button
							class="search-footer-item"
							text="搜索"
							type="primary"
							@click="searchSubmit"
							icon="search"></up-button>
						<up-button
							class="search-footer-item"
							@click="resetSubmit"
							text="重置"></up-button>
					</view>
				</view>
			</scroll-view>
		</up-popup>
		<view class="content">
			<!--内容-->
			<view class="table">
				<ty-table
					:data="tableList"
					:columns="columns"
					:sortable="true"
					@row-click="rowClick"
					:cellClassName="() => 'table-cell'"
					:headerCellClassName="() => 'table-cell'"
					:rowClassName="() => 'table-row'"
					:isOperateBtn="(row) => {
						return (checkPermi(['zhouyz:orderRefund:edit']) && row.status === 0) ||
						// (checkPermi(['zhouyz:orderRefund:storage']) && row.status === 2) ||
						(checkPermi(['zhouyz:orderRefund:audit']) && row.status === 2) ||
						(checkPermi(['zhouyz:orderRefund:return']) && row.status > 1 && row.status < 5) ||
						(checkPermi(['zhouyz:orderRefund:payment']) && row.status === 4)
					}"
					stripe
					border>
					<template v-slot:before="{ row }">
						<view class="table-td-before table-cell table-td-row">
							<view class="table-td-row">退款单号：<text class="table-td-text">{{row.number}}</text></view>
							<view class="table-td-row">订单编号：<text class="table-td-text">{{row.orderNumber}}</text></view>
						</view>
					</template>
					<template v-slot:order="{ row }">
						<view class="table-td-cell">
							<view class="table-td-row">区域：<text class="table-td-text">{{arrText(setStore.allProvinceList, row.regionCode)}}</text></view>
							<view class="table-td-row">仓库：<text class="table-td-text">{{row.warehouseName}}</text></view>
							<view class="table-td-row">销售：<text class="table-td-text">{{row.salesmanName}}</text></view>
						</view>
					</template>
					<template v-slot:user="{ row }">
						<view class="table-td-cell">
							<view class="table-td-row">姓名：<text class="table-td-text">{{row.customerName}}</text></view>
							<view class="table-td-row">电话：<text class="table-td-text">{{row.accountPhone}}</text></view>
							<view class="table-td-row">店铺：<text class="table-td-text">{{row.company}}</text></view>
							<view class="table-td-row">销售：<text class="table-td-text">{{row.salesmanName}}</text></view>
						</view>
					</template>
					<template v-slot:money="{ row }">
						<view class="table-td-cell">
							<view class="table-td-row">实付：<text class="table-td-text">{{row.actualPrice}}</text></view>
							<view class="table-td-row">已退：<text class="table-td-text">{{row.refundTotal}}</text></view>
							<view class="table-td-row">现退：<text class="table-td-text">{{row.refundCurrent}}</text></view>
						</view>
					</template>
					<template v-slot:status="{ row }">
						<view class="table-td-cell">
							<view class="table-td-row">发起人：<text class="table-td-text">{{row.createUserName}}</text></view>
							<view class="table-td-row">
								状态：
								<uni-tag size="small" :text="arrText(orderRefundStatusList, row.status)" />
							</view>
							<view class="table-td-row">备注：<text class="table-td-text">{{row.remark}}</text></view>
						</view>
					</template>
					<template v-slot:operate="{ row }">
						<view class="table-td-operate table-cell table-td-row main-flex">
							<view
								class="table-operate-btn main-flex"
								v-if="checkPermi(['zhouyz:orderRefund:edit']) && row.status === 0">
								<up-button
									size="mini"
									type="primary"
									@click.stop="editFun(row)">
									<uni-icons class="table-operate-btn-icon" type="compose" size="24rpx" color="#ffffff"></uni-icons>
									<view class="table-operate-btn-text">修改</view>
								</up-button>
							</view>
							<!-- <view
								class="table-operate-btn main-flex"
								v-if="checkPermi(['zhouyz:orderRefund:storage']) && row.status === 2">
								<up-button
									size="mini"
									type="primary"
									@click.stop="editFun(row)">
									<uni-icons class="table-operate-btn-icon" type="compose" size="24rpx" color="#ffffff"></uni-icons>
									<view class="table-operate-btn-text">确认入库</view>
								</up-button>
							</view> -->
							
							<view
								class="table-operate-btn main-flex"
								v-if="checkPermi(['zhouyz:orderRefund:audit']) && row.status === 2">
								<up-button
									size="mini"
									type="primary"
									@click.stop="editFun(row)">
									<uni-icons class="table-operate-btn-icon" type="compose" size="24rpx" color="#ffffff"></uni-icons>
									<view class="table-operate-btn-text">确认无误</view>
								</up-button>
							</view>
							
							<view
								class="table-operate-btn main-flex"
								v-if="checkPermi(['zhouyz:orderRefund:return']) && row.status > 1 && row.status < 5">
								<up-button
									size="mini"
									type="primary"
									@click.stop="editFun(row)">
									<uni-icons class="table-operate-btn-icon" type="compose" size="24rpx" color="#ffffff"></uni-icons>
									<view class="table-operate-btn-text">退回重填</view>
								</up-button>
							</view>
							
							<view
								class="table-operate-btn main-flex"
								v-if="checkPermi(['zhouyz:orderRefund:payment']) && row.status === 4">
								<up-button
									size="mini"
									type="primary"
									@click.stop="editFun(row)">
									<uni-icons class="table-operate-btn-icon" type="compose" size="24rpx" color="#ffffff"></uni-icons>
									<view class="table-operate-btn-text">退款</view>
								</up-button>
							</view>
							
						</view>
					</template>
					<template v-slot:empty>
						<view class="table-empty">
							<up-empty mode="data"/>
						</view>
					</template>
				</ty-table>
			</view>
			<!--分页-->
			<view class="pagination" v-show="total > 0">
				<ty-pagination
					:total="total"
					:pageSize="pageSize"
					:currentPage="pageNum"
					:pageSizes="[10, 20, 30, 50]"
					mode="simple"
					layout="first,prev,page,next,last"
					@sizeChange="sizeChange"
					@change="pageChange">
				</ty-pagination>
			</view>
		</view>
	</view>
</template>

<script setup>
import { onLoad } from '@dcloudio/uni-app';
import { computed, onMounted, ref } from 'vue';
import { tab, auth, modal } from "@/plugins"
import { getTitle } from "@/utils/getter"
import { checkPermi, checkRole, checkOnlyPermi, checkOnlyRole } from "@/utils/permission"
import { allProvince } from "@/api/erp_api/region"
import { orderRefund } from "@/api/erp_api/order"
import { isObjectEmptySimple, copyText, arrText } from '@/utils/ty.js';
import { orderRefundStatusList } from '@/utils/config.js';
import useUserStore from '@/store/modules/user'
import useSetStore from '@/store/modules/set'

	const userStore = useUserStore()	
	const setStore = useSetStore()

	// 标题设置
	const title = ref('')
	onLoad((option) => {
		title.value = getTitle(userStore.menu)
		uni.setNavigationBarTitle({
			title: title.value 
		})
		uni.$on('refreshOrderList', () => {
			getList()
		})
	})
	
	// 搜索相关
	const isSearchNull = ref(true)
	const isOpen =  ref(false)
	const clickSearch = (val) => {
		isOpen.value = val
	}
	const closeSearch = (val) => {
		isOpen.value = false
	}
	// 确认搜索
	const searchRef = ref(null)
	const searchModel = ref({
		regionCode: '',
		number: '',
		warehouseName: '',
		orderNumber: '',
		salesmanName: '',
		status: ''
	})
	const searchSubmit = () => {
		pageNum.value = 1
		tableList.value = []
		isOpen.value = false
		searchFirst()
	}
	// 重置搜索
	const resetSubmit = () => {
		// searchRef.value.resetFields()
		searchModel.value = {
			regionCode: '',
			number: '',
			warehouseName: '',
			orderNumber: '',
			salesmanName: '',
			status: ''
		}
		if (userStore.regionCode) {
			searchModel.value.regionCode = userStore.regionCode
		}
		isSearchNull.value = true
		isOpen.value = false
		searchFirst()
	}
	
	
	// 数据查询
	const pageNum = ref(1)
	const pageSize = ref(10)
	const total = ref(0)
	const tableList = ref([])
	const isLoading = ref(false)
	
	// 表格头部
	const columns = ref([
		{ title: '订单信息', key: 'order' },
		{ title: '订单金额', key: 'money', width: '160rpx' },
		{ title: '状态备注', key: 'status', width: '280rpx' }
	])
	
	// 从第一页查询
	const searchFirst = () => {
		pageNum.value = 1
		tableList.value = []
		isSearchNull.value = isObjectEmptySimple(searchModel.value)
		getList()
	}
	//页数切换
	const pageChange = (page) => {
		pageNum.value = page
		getList()
	}
	//每页条数切换
	const sizeChange = (size) => {
		pageSize.value = size
		getList()
	}
	
	const getList = () => {
		const params = {
			pageNum: pageNum.value,
			pageSize: pageSize.value
		}
		if (searchModel.value.regionCode) {
			params.regionCode = searchModel.value.regionCode
		}
		if (searchModel.value.number) {
			params.number = searchModel.value.number
		}
		if (searchModel.value.warehouseName) {
			params.warehouseName = searchModel.value.warehouseName
		}
		if (searchModel.value.orderNumber) {
			params.orderNumber = searchModel.value.orderNumber
		}
		if (searchModel.value.salesmanName) {
			params.salesmanName = searchModel.value.salesmanName
		}
		if (searchModel.value.status || searchModel.value.status === 0) {
			params.status = searchModel.value.status
		}
		// if (userStore.warehouseId) {
		// 	params.warehouseId = userStore.warehouseId
		// }
		isLoading.value = true
		uni.showLoading({
			title: '数据加载中...'
		})
		orderRefund(params).then(res => {
			tableList.value = res.rows || []
			total.value = res.total
			isLoading.value = false
			uni.hideLoading()
    }).catch(() => {
			isLoading.value = false
			uni.hideLoading()
		})
	}
	
	// 点击行
	const rowClick = (row) => {
		uni.navigateTo({
			url: `/pages/order/orderRefundDetail?id=${row.id}`
		})
	}
	
	// 修改
	const editFun = (row) => {
		uni.navigateTo({
			url: `/pages/order/orderRefundAdd?id=${row.id}`
		})
	}
 	
	onMounted(async () => {
		if (userStore.regionCode) {
			searchModel.value.regionCode = userStore.regionCode
		}
		searchFirst()
	})
</script>

<style scoped>
</style>
